package com.eachonline.smart.web.banner;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.model.BaseModel;
import com.eachonline.oss.OssConstant;
import com.eachonline.oss.OssService;
import com.eachonline.smart.model.SmartBanner;
import com.eachonline.smart.service.BannerService;
import com.eachonline.util.DateUtil;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/banner")
public class BannerController{
	
	@Resource
	private BannerService bannerService;

	@Resource
	private OssService ossService;
	
	
	/**
	 * 查询列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryBannerList",method = RequestMethod.POST)
	public JsonResult queryBannerList(HttpServletRequest request,String site){
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=100;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
	    Map<String,Object> returnMap = new HashMap<String,Object>();
		Pagination<SmartBanner> bannerPageInfo = bannerService.queryBannerList(begin, pageSize, site);
		returnMap.put("bannerPageInfo", bannerPageInfo);
		return new JsonResult(true,returnMap);
	}
	
	

	/**
	 * 管理后台查询列表
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/querySysBannerList",method = RequestMethod.POST)
	public JsonResult querySysBannerList(HttpServletRequest request,int start,int pageSize,String site){
	    Map<String,Object> returnMap = new HashMap<String,Object>();
		Pagination<SmartBanner> bannerPageInfo = bannerService.queryBannerList(start, pageSize, site);
		returnMap.put("bannerPageInfo", bannerPageInfo);
		return new JsonResult(true,returnMap);
	}
	
	
	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public JsonResult saveEntity(SmartBanner entity,HttpServletResponse response){
		
		
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		try {
			if(StringUtil.isEmpty(entity.getSid()))
			{
				entity.setSid(UUIDUtil.getUUID());
				bannerService.insert(entity);
			}else
			{
				bannerService.updateByPrimaryKeySelective(entity);
			}
			
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	
	/**
	 * 上传banner图片接口
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadBannerImage",method = RequestMethod.POST)
	public JsonResult uploadBannerImg(@RequestParam(value="bannerFile", required = false)CommonsMultipartFile bannerFile,HttpServletResponse response){
		

    	response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		if(bannerFile.isEmpty())
			{
				return  new JsonResult(false, "图片不能为空 ");
			}
	    	String picKey="";
         try { 
         	StringBuffer key=new StringBuffer();
         	key.append("banner/");
         	key.append(DateUtil.formatPureStandardDate(new Date()));
         	key.append("/");
         	key.append(bannerFile.getOriginalFilename());
         	 picKey=key.toString();
         	ossService.upload(OssConstant.NAMESPACE_BUSINESS,picKey , bannerFile);
         
         } catch (Exception e) {  
             e.printStackTrace();  
             System.out.println("上传出错");  
         } 
         Map<String,Object> dataMap=new HashMap<String, Object>();
         dataMap.put("picKey", picKey);
         return  new JsonResult(true, "上传成功 ",dataMap);
	}
	
	/**
	 * 删除banner图片接口
	 * @param pickey
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteBannerImg",method = RequestMethod.POST)
	public JsonResult deleteBannerImg(String pickey,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		ossService.delete(OssConstant.NAMESPACE_BUSINESS,pickey);
		return new JsonResult(true, "删除成功!");
	}
	
	/**
	 * 编辑
	 */
	@ResponseBody
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public JsonResult updateEntity(SmartBanner entity,HttpServletResponse response) {
		try {
			bannerService.updateByPrimaryKeySelective(entity);
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	/**
	 * 查询
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/query",method = RequestMethod.POST)
	public JsonResult queryEntity(String sid,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		try {
			return new JsonResult(true,bannerService.selectByPrimaryKey(sid));
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public JsonResult deleteEntities(String ids,HttpServletResponse response){
		
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		try {
			bannerService.deleteByPrimaryKey(ids);
			return new JsonResult(true,"操作成功");
		} catch (Exception e) {
			return new JsonResult(false,e.getMessage());
		}
	}

	
}
